package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import aes.client.AesClient;
import aes.general.GuiControlBeta;

/**
 * This is the main client login frame. 
 * Using this user interface allows the user to connect 
 * to the system.
 */
public class LoginFrame extends JFrame  {


	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanel mainpanel;
	private JLabel lblServerIp;
	private JLabel lblServerPort;
	private JLabel lblUserId;
	private JLabel lblPassword;
	private JLabel lblClientLogin;
	private JButton btnConnect;
	private JButton btnClear;
	private JTextField ipTextField;
	private JTextField portTextField;
	private JTextField userIdTextField;
	private JPasswordField passTextField;
	private JSeparator separator;
	
	protected AesClient client;
	private String pass;
	private String userid;

	/**
	 * Create the application.
	 */
	public LoginFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 512, 446);
		setTitle("Client");
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0,0));
		
		mainpanel = new JPanel();
		contentPane.add(mainpanel,"mainpanel");
		mainpanel.setLayout(null);
		
		lblServerIp = new JLabel("Server IP");
		lblServerIp.setBounds(151, 119, 86, 20);
		mainpanel.add(lblServerIp);

		lblServerPort = new JLabel("Server Port");
		lblServerPort.setBounds(151, 144, 86, 20);
		mainpanel.add(lblServerPort);
		
		lblUserId = new JLabel("User ID");
		lblUserId.setBounds(151, 169, 86, 20);
		mainpanel.add(lblUserId);
		
		lblPassword = new JLabel("Password");
		lblPassword.setBounds(151, 194, 86, 20);
		mainpanel.add(lblPassword);
		
		ipTextField = new JTextField("localhost");
		ipTextField.setBounds(251, 119, 86, 20);
		ipTextField.setColumns(10);
		mainpanel.add(ipTextField);
		
		portTextField = new JTextField("3606");
		portTextField.setBounds(251, 144, 86, 20);
		portTextField.setColumns(10);
		mainpanel.add(portTextField);

		userIdTextField = new JTextField("3");
		userIdTextField.setBounds(251, 169, 86, 20);
		userIdTextField.setColumns(10);
		mainpanel.add(userIdTextField);

		passTextField = new JPasswordField("3");
		passTextField.setBounds(251, 194, 86, 20);
		passTextField.setColumns(10);
		mainpanel.add(passTextField);
		
		btnConnect = new JButton("Connect");
		btnConnect.setBounds(151, 266, 94, 23);
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				connectToAes();		
			}
		});
		
		mainpanel.add(btnConnect);
		/*
		 * Clear Button Action Listener
		 */
		btnClear = new JButton("Clear");
		btnClear.setBounds(251, 266, 92, 23);
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				ipTextField.setText(null);
				portTextField.setText(null);
				passTextField.setText(null);
				userIdTextField.setText(null);
			}
		});
		mainpanel.add(btnClear);
		
		lblClientLogin = new JLabel("Client Login");
		lblClientLogin.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		lblClientLogin.setBounds(183, 11, 129, 29);
		mainpanel.add(lblClientLogin);
		
		separator = new JSeparator();
		separator.setBounds(10, 41, 476, 2);
		mainpanel.add(separator);
	}

	/**
	 * Connect to AES server.
	 */
	@SuppressWarnings("deprecation")
	// password field get text;
	protected void connectToAes() {
		String ip = ipTextField.getText();
		try {
			Integer port = Integer.decode(portTextField.getText());
			pass = passTextField.getText();
			userid = userIdTextField.getText();

			client = new AesClient(ip, port);
			GuiControlBeta.CheckUserRequestToServer(userid, pass, client);

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,
					"Login Failed ,  Wrong Ip or Port.", "Error",
					JOptionPane.ERROR_MESSAGE);
			return;	
		}
		dispose();
	}
}
